"""自定义异常模块"""

from typing import Optional


class TextDetectionException(Exception):
    """文本检测基础异常"""
    
    def __init__(self, message: str, error_code: Optional[str] = None):
        self.message = message
        self.error_code = error_code
        super().__init__(self.message)


class PlatformNotSupportedException(TextDetectionException):
    """不支持的检测平台异常"""
    
    def __init__(self, platform: str):
        message = f"不支持的检测平台: {platform}"
        super().__init__(message, "PLATFORM_NOT_SUPPORTED")


class TextTooLongException(TextDetectionException):
    """文本过长异常"""
    
    def __init__(self, length: int, max_length: int):
        message = f"文本长度 {length} 超过最大限制 {max_length}"
        super().__init__(message, "TEXT_TOO_LONG")


class TextTooShortException(TextDetectionException):
    """文本过短异常"""
    
    def __init__(self, length: int, min_length: int):
        message = f"文本长度 {length} 低于最小限制 {min_length}"
        super().__init__(message, "TEXT_TOO_SHORT")


class PlatformAPIException(TextDetectionException):
    """平台API调用异常"""
    
    def __init__(self, platform: str, message: str):
        full_message = f"平台 {platform} API调用失败: {message}"
        super().__init__(full_message, "PLATFORM_API_ERROR")


class TaskNotFoundException(TextDetectionException):
    """任务未找到异常"""
    
    def __init__(self, task_id: str):
        message = f"任务 {task_id} 未找到"
        super().__init__(message, "TASK_NOT_FOUND")


class InvalidTaskStatusException(TextDetectionException):
    """无效任务状态异常"""
    
    def __init__(self, task_id: str, status: str):
        message = f"任务 {task_id} 状态 {status} 无效"
        super().__init__(message, "INVALID_TASK_STATUS")


class RateLimitExceededException(TextDetectionException):
    """请求频率限制异常"""
    
    def __init__(self, retry_after: Optional[int] = None):
        message = "请求频率超过限制"
        if retry_after:
            message += f"，请在 {retry_after} 秒后重试"
        super().__init__(message, "RATE_LIMIT_EXCEEDED")


class AuthenticationException(TextDetectionException):
    """认证异常"""
    
    def __init__(self, message: str = "认证失败"):
        super().__init__(message, "AUTHENTICATION_FAILED")


class AuthorizationException(TextDetectionException):
    """授权异常"""
    
    def __init__(self, message: str = "权限不足"):
        super().__init__(message, "AUTHORIZATION_FAILED") 